package com.share.admin.web;

import com.share.admin.config.security.SecurityContext;
import com.share.admin.model.db.Role;
import com.share.admin.model.vo.RoleAddOrUpdateReq;
import com.share.admin.model.vo.RolePageReq;
import com.share.admin.service.IRoleService;
import com.share.common.common.Page;
import com.share.common.common.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-09-22
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService iRoleService;

    /**
     * 获取角色列表
     */
    @RequestMapping("/list")
    public Result list() {
        Set<Role> set = null;
        if (SecurityContext.isAdmin()) {
            set = iRoleService.listAll();
        } else {
            Long userId = SecurityContext.getUserId();
            set = iRoleService.listByUserId(userId);
        }
        return Result.success(set);
    }

    @ApiOperation("角色绑定或者移除菜单")
    @RequestMapping(value = "/bindMenus")
    public Result bindOrRemoveMenus(Boolean isBind, String roleId, String menuIds) {
        if (isBind == null || StringUtils.isBlank(roleId)) {
            return Result.failure("isBind,roleId参数异常!");
        }
        List<String> list = null;
        if (StringUtils.isNotEmpty(menuIds)) {
            list = Arrays.stream(menuIds.split(",")).collect(Collectors.toList());
        }

        if (isBind && CollectionUtils.isEmpty(list)) {
            return Result.failure("menuIds值异常,没有可绑定的菜单!");
        }
        return Result.success(iRoleService.bindOrRemoveMenus(isBind, roleId, list));
    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "/pageList")
    public Result resultPageList(RolePageReq req) {
        Page page = iRoleService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("新增")
    @RequestMapping("/add")
    public Result add(RoleAddOrUpdateReq req) {
        Role record = new Role();
        BeanUtils.copyProperties(req, record);
        return Result.bool(iRoleService.save(record));
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    public Result update(RoleAddOrUpdateReq req) {
        if (req.getId() == null) {
            return Result.failure("参数异常!");
        }
        Role record = new Role();
        BeanUtils.copyProperties(req, record);
        return Result.bool(iRoleService.updateById(record));
    }

    @ApiOperation("删除")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return Result.bool(iRoleService.removeById(id));
    }
}
